package dao

import (
	"gorm.io/gorm"
	"kf/models/entity"
	"time"
)

type WelcomeDao struct {
	DB *gorm.DB
}

func (welcomesDao *WelcomeDao) CreateWelcome(userId string, content string) uint {
	if userId == "" || content == "" {
		return 0
	}
	w := &entity.Welcome{
		UserId:  userId,
		Content: content,
		Ctime:   time.Now(),
		Keyword: "config",
	}
	welcomesDao.DB.Create(w)
	return w.ID
}
func (welcomesDao *WelcomeDao) UpdateWelcome(userId string, id string, content string) uint {
	if userId == "" || content == "" {
		return 0
	}
	w := &entity.Welcome{
		Content: content,
	}
	welcomesDao.DB.Model(w).Where("user_id = ? and id = ?", userId, id).Updates(w)
	return w.ID
}
func (welcomesDao *WelcomeDao) FindWelcomeByUserIdKey(userId interface{}, keyword interface{}) entity.Welcome {
	var w entity.Welcome
	welcomesDao.DB.Where("user_id = ? and keyword=?", userId, keyword).First(&w)
	return w
}
func (welcomesDao *WelcomeDao) FindWelcomesByUserId(userId interface{}) []entity.Welcome {
	var w []entity.Welcome
	welcomesDao.DB.Where("user_id = ?", userId).Find(&w)
	return w
}
func (welcomesDao *WelcomeDao) FindWelcomesByKeyword(userId interface{}, keyword interface{}) []entity.Welcome {
	var w []entity.Welcome
	welcomesDao.DB.Where("user_id = ? and keyword=?", userId, keyword).Find(&w)
	return w
}
func (welcomesDao *WelcomeDao) DeleteWelcome(userId interface{}, id string) {
	welcomesDao.DB.Where("user_id = ? and id = ?", userId, id).Delete(entity.Welcome{})
}
